<template>
  <div>
    <div class="center-middle" style="color: #303133;text-align: center;"><h2>📣📢欢迎使用 公告栏管理系统📣📢</h2></div>

    <!--搜索栏-->
    <div style="margin-top: 20px;">
      <el-form>
        <!--搜索栏-->
        <el-form-item label="搜索内容:">
          <el-input style="width: 200px" placeholder="请输入标题" suffix-icon="el-icon-search" v-model="name"></el-input>
          <el-button style="margin-left: 20px" icon="el-icon-search" type="primary" @click="load">搜索</el-button>
          <el-button type="info" icon="el-icon-refresh" @click="reset">重置</el-button>
          <!--批量删除-->
          <el-button style="margin-left: 30px" type="success" @click="handleAdd" icon="el-icon-circle-plus-outline">新增</el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定批量删除这些数据吗？"
              @confirm="delBatch"
          >
            <el-button type="danger" style="margin-left: 10px" slot="reference" icon="el-icon-remove-outline">批量删除</el-button>
          </el-popconfirm>
        </el-form-item>
      </el-form>

    </div>


    <!--表格-->
    <el-table :data="tableData"
              border
              stripe
              :header-cell-class-name="'headerBg'"
              @selection-change="handleSelectionChange"
    >
      <el-table-column
          align="center"
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          align="center"
          prop="id"
          label="ID"
          width="80"
          sortable>
      </el-table-column>
      <el-table-column
          align="center"
          prop="name"
          label="标题">
      </el-table-column>
      <el-table-column
          align="center"
          prop="content"
          label="内容">
        <template v-slot="scope">
          <el-button size="mini" @click="view(scope.row.content)">查看内容</el-button>
        </template>
      </el-table-column>
      <el-table-column
          align="center"
          prop="time"
          label="发布时间">
      </el-table-column>
<!--      <el-table-column
          align="center"
          prop="user"
          label="发布人">
      </el-table-column>-->
      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="scope">
          <el-button
              icon="el-icon-edit"
              size="mini"
              type="success"
              @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)">
            <el-button style="margin-left: 5px" icon="el-icon-remove-outline" size="mini" type="danger" slot="reference">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <div style="padding: 10px 0; float: right">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>

    </div>

    <!--编辑或新增信息-->
    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="60%" :close-on-click-modal="false">

      <el-form label-width="100px" size="small" style="width: 90%">
        <el-form-item label="标题">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <div id="richText"></div>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>

    </el-dialog>

    <!--将富文本内容渲染到页面-->
    <el-dialog title="内容" :visible.sync="dialogFormVisible1" width="60%" :close-on-click-modal="false">
      <el-card>
        <div v-html="content"></div>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor"

let editor;
export default {
  name: "Notices",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      dialogFormVisible: false,
      dialogFormVisible1: false,
      content: '',
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.load()
  },
  methods: {
    view(content) {
      this.content = content
      this.dialogFormVisible1 = true
    },
    load() {
      const _this = this
      axios.get('http://localhost:9090/notices/page', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          type: 2
        }
      }).then(function (resp) {
        _this.tableData = resp.data.data.records
        _this.total = resp.data.data.total
      })
    },
    save() {
      const content = editor.txt.html()
      console.log(content)
      // 注意：这个地方需要手动赋值
      this.form.content = content
      this.form.type = 2
      axios.post('http://localhost:9090/notices', this.form).then(res => {
        if (res.data.code === 0) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })

    },
    handleAdd() {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if (!editor) {
          editor = new E("#richText")
          editor.create()
        }
        editor.txt.html('')  // 清除内容
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if (!editor) {
          editor = new E("#richText")
          editor.create()
        }
        editor.txt.html(this.form.content)
      })
    },
    del(id) {
      axios.delete("http://localhost:9090/notices/" + id).then(res => {
        if (res.data.code === 0) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择需要删除的数据")
        return
      }
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      axios.post("http://localhost:9090/notices/del/batch", ids).then(res => {
        if (res.data.code === 0) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
  }
}
</script>


<style>

</style>

